/*
Author:HuZhixin
Date:2020-06-19
 */

const user=require('../models/user');

var userModel=user.userModel;

//1.add(注册)
async function addUser(user) {
    try{

        var ret=await userModel.create(user);//create方法返回对象
        // console.log(user);
        // console.log(ret);

        if(user=={}||user==undefined){
            return {success:false,msg:'用户保存失败'}
        }else {
            return {success:true,user:ret};
        }

    }catch (e) {
        // console.log('add user is error--error reason:'+e);
        return {success:false,msg:e}
    }

}

//2.findbyName
async function findByName(name) {
     try {
         var user=await userModel.findOne({name:name});
         if(user==undefined||user=={}){
             return {success:false,msg:'查无此人'};
         }else {
             return {success:true,user:user};
         }
     }catch (e) {
         return{success:false,msg:e};
     }
};

exports.addUser=addUser;
exports.findByName=findByName;